package com.auxgroup.smarthome.job.timingcontrol;

import com.alibaba.fastjson.JSON;
import com.auxgroup.smarthome.enterprise.service.DeviceManageService;
import com.auxgroup.smarthome.entity.AppScheduleExtraEntity;
import com.auxgroup.smarthome.repo.AppScheduleRedisRepo;
import com.auxgroup.smarthome.utils.common.LOG;
import com.auxgroup.smarthome.web.ApiResponse;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by fju on 2017/8/16.
 */
@Component
public class AppScheduleJob implements SimpleJob {

    @Autowired
    private AppScheduleRedisRepo appScheduleRedisRepo;

    @Autowired
    protected DeviceManageService deviceManageService;


    @Override
    public void execute(ShardingContext shardingContext) {
        List<AppScheduleExtraEntity> appScheduleList = appScheduleRedisRepo.getPartDataList(shardingContext.getShardingItem(),AppScheduleExtraEntity.class);
        LOG.info(this,"app定时控制任务 -- 分片No." + shardingContext.getShardingItem()
                + "获取定时控制任务缓存数据长度："+appScheduleList.size());
        if(appScheduleList == null || appScheduleList.size() == 0) return;
        appScheduleList.stream().forEach(appScheduleEntity -> {
            if(appScheduleEntity.inRepeatRule() && appScheduleEntity.atTime()){
                shcudleControl(appScheduleEntity);
            }
        });
    }

    /**
     * 定时控制
     * @param appScheduleExtraEntity
     * @return
     */
    private void shcudleControl(AppScheduleExtraEntity appScheduleExtraEntity){
        Map<String, String> controlMap = getControlMap(appScheduleExtraEntity);
        if(appScheduleExtraEntity.isHomeDevice()){
            LOG.info(this,"开始执行控制指令==========家用设备");
            ApiResponse apiResponse = deviceManageService.commonRemoteControlDevice(appScheduleExtraEntity.getProductKey(),appScheduleExtraEntity.getDid(),controlMap);
            LOG.info(this, "家用空调控制结果："+ JSON.toJSONString(apiResponse));
            LOG.info(this,"执行控制指令结束==========家用设备");
        }else{
            controlMap = getControlMap(appScheduleExtraEntity);
            LOG.info(this,"执行控制指令结束==========商用用设备");
            ApiResponse apiResponse = deviceManageService.businessRemoteControlDevice(appScheduleExtraEntity.getProductKey(),appScheduleExtraEntity.getDid(),appScheduleExtraEntity.getDst(), controlMap);
            LOG.info(this, "商用空调控制结果："+ JSON.toJSONString(apiResponse));
            LOG.info(this,"执行控制指令结束==========商用用设备");
        }
    }

    /**
     *  家用空调与商用空调相同
     *  key:  模式,     0:自动模式  1:制冷模式  2:制热模式   3:除湿模式  4:送风模式
     * value: 协 议,    0:自动模式  1:制冷模式  4:制热模式   2:除湿模式  6:送风模式
     */
    private static final Map<Integer,Integer> modeMap = new HashMap<>();
    static {
        modeMap.put(0,0);//自动模式
        modeMap.put(1,1);//制冷模式
        modeMap.put(2,4);//制热模式
        modeMap.put(3,2);//除湿模式
        modeMap.put(4,6);//送风模式
    }

    /**
     *  家用挂式空调
     *   key: 风速, 0:低风  1:中风  2:高风  3:静音 4:自动 5:强力
     * value: 挂机：3:低风  2:中风  1:高风  5:自动
     */
    private static final Map<Integer,Integer> windGuaMap = new HashMap<>();
    static {
        windGuaMap.put(0,3);//低风
        windGuaMap.put(1,2);//中风
        windGuaMap.put(2,1);//高风
        windGuaMap.put(4,5);//自动
    }

    /**
     * 家用柜式空调
     *     key: 风速, 0:低风  1:中风  2:高风  3:静音 4:自动 5:强力
     *   value: 柜机, 3:低风  1:中风  5:高风  7:自动
     */
    private static final Map<Integer,Integer> windGuiMap = new HashMap<>();
    static {
        windGuiMap.put(0,3);//低风
        windGuiMap.put(1,1);//中风
        windGuiMap.put(2,5);//高风
        windGuiMap.put(4,7);//自动
    }

    /**
     * 商用空调风速
     *   key: 风速, 0:低风  1:中风  2:高风  3:静音 4:自动 5:强力
     *   value:   1：高风  2：中风  3：低风  5：自动
     */
    private static final Map<Integer,Integer> windBusinessMap = new HashMap<>();
    static {
        windBusinessMap.put(0,3);//低风
        windBusinessMap.put(1,2);//中风
        windBusinessMap.put(2,1);//高风
        windBusinessMap.put(4,5);//自动
    }


    //                  3:静音   5:强力
    //0:无静音、无强力    2:静音   1:强力 家用与商用一样

    /**
     * 数据点控制点数值构造
     * @param appScheduleExtraEntity
     * @return
     */
    public Map<String,String> getControlMap(AppScheduleExtraEntity appScheduleExtraEntity){
        boolean isHomeDevice = appScheduleExtraEntity.isHomeDevice();
        Map<String,String> controlMap = new HashMap<>();
        Integer deviceOperate = appScheduleExtraEntity.getDeviceOperate();
        controlMap.put("on_off",deviceOperate.toString());
        Integer temperature = appScheduleExtraEntity.getTemperatureSetting();
        Integer mode = appScheduleExtraEntity.getMode();
        Integer windSpeed = appScheduleExtraEntity.getWindSpeed();
        // 商用空调数据点控制
        boolean flag = false;
        if (windSpeed == 3){
            controlMap.put("turbo_silence","2");
            flag = true;
        }
        if (windSpeed == 5){
            controlMap.put("turbo_silence","1");
            flag = true;
        }
        if (!flag){
            controlMap.put("turbo_silence","0");
            if (isHomeDevice){
                if (appScheduleExtraEntity.isUseGuaDevice()){
                    controlMap.put("wind_speed_1",windGuaMap.get(windSpeed).toString());
                }else{
                    controlMap.put("wind_speed_1",windGuiMap.get(windSpeed).toString());
                }
            } else{
                    controlMap.put("wind_speed_1",windBusinessMap.get(windSpeed).toString());
            }
        }
        if (deviceOperate==0){
            if(isHomeDevice) {
                controlMap.put("turbo_silence", "0");
                controlMap.put("sleep_mode", "0");
                controlMap.put("electric_heating", "0");
                controlMap.put("eco", "0");
                controlMap.put("healthy", "0");
            } else {
                controlMap.put("turbo_silence", "0");
                controlMap.put("sleep_mode", "0");
                controlMap.put("electric_heating", "0");
            }
        }
        if (deviceOperate==1){
            controlMap.put("air_con_func",modeMap.get(mode).toString());
            controlMap.put("temperature",temperature.toString());
            if (isHomeDevice) {
                controlMap.put("clean","0");
                // modify by qiuhua: 开机指令显示温度为整数，关闭0.5度温度设置
                controlMap.put("half", "0");
                // 制热模式，开启电加热
                if(mode == 2)
                    controlMap.put("electric_heating", "1");
            }
        }
        return controlMap;
    }





}
